package cn.s1995.sloth.bms.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.s1995.sloth.common.core.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cn.s1995.sloth.common.log.annotation.SysLog;
import cn.s1995.sloth.bizs.entity.UserVisitorRecord;
import cn.s1995.sloth.bizs.service.UserVisitorRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 用户访问记录
 *
 * @author Lex
 * @date 2024-05-25 11:02:45
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userVisitorRecord")
@Tag(description = "UserVisitorRecord", name = "用户访问记录管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class UserVisitorRecordController {

    private final UserVisitorRecordService userVisitorRecordService;

    /**
     * 获取分页
     * @param page
     * @param req
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_view')")
    public R<Page<UserVisitorRecord>> getPage(@ParameterObject Page page, @ParameterObject UserVisitorRecord req) {
        return R.ok(userVisitorRecordService.page(page, buildQueryWrapper(req)));
    }

    /**
     * 获取列表
     * @param req
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_view')")
    public R<List<UserVisitorRecord>> getList(@ParameterObject UserVisitorRecord req) {
        return R.ok(userVisitorRecordService.list(buildQueryWrapper(req)));
    }

    /**
     * 获取对象
     * @param id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id获取")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(userVisitorRecordService.getById(id));
    }

    /**
     * 新增
     * @param req
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增", description = "新增")
    @SysLog("新增用户访问记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_add')")
    public R save(@RequestBody UserVisitorRecord req) {
        return R.ok(userVisitorRecordService.save(req));
    }

    /**
     * 修改
     * @param req
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改", description = "修改")
    @SysLog("修改用户访问记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_edit')")
    public R updateById(@RequestBody UserVisitorRecord req) {
        return R.ok(userVisitorRecordService.updateById(req));
    }

    /**
     * 删除
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "删除", description = "删除")
    @SysLog("删除用户访问记录")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(userVisitorRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID集合
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_UserVisitorRecord_export')")
    public List<UserVisitorRecord> export(UserVisitorRecord req, Long[] ids) {
        return userVisitorRecordService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), UserVisitorRecord::getId, ids));
    }

	/**
     * 构建查询条件
     * @param req
     * @return
     */
    @NotNull
    private static LambdaQueryWrapper<UserVisitorRecord> buildQueryWrapper(UserVisitorRecord req) {
        LambdaQueryWrapper<UserVisitorRecord> wrapper = Wrappers.lambdaQuery(UserVisitorRecord.class);
		if (req.hasRangeTime()) {
           	wrapper.between(UserVisitorRecord::getCreateTime, req.getRangeTime()[0], req.getRangeTime()[1]);
        }
		wrapper.and(
                StrUtil.isNotBlank(req.getSearch()),
                w -> w.like(UserVisitorRecord::getId, req.getSearch())
        );

		wrapper.eq(Objects.nonNull(req.getUid()), UserVisitorRecord::getUid, req.getUid());
        return wrapper;
    }

}
